package com.atguigu.hbase.util;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @ClassName NameSpaceUtils
 * @DeScription Hbase NameSpase(库)工具类
 * @Author 剧情再美终是戏
 * @Date 2019/12/7 13:43
 * @Version 1.0
 **/
public class NameSpaceUtils {

    private NameSpaceUtils() {
    }

    private static Logger logger = LoggerFactory.getLogger(NameSpaceUtils.class);

    /**
     * 创建库
     *
     * @param conn Hbase连接
     * @param name 库的名称
     * @return boolean
     * @Author 剧情再美终是戏
     * @Date 2019/12/7
     * @Version 1.0
     **/
    public static boolean createNs(Connection conn, String name) throws IOException {
        if (null == conn || StringUtils.isBlank(name)) {
            logger.error("com.atguigu.hbase.util.NameSpaceUtils.createNs Method Param Error : conn, name  is require!");
            return false;
        }

        try (Admin admin = conn.getAdmin()) {
            NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(name).build();
            admin.createNamespace(namespaceDescriptor);
            return true;
        } catch (Exception e) {
            logger.error("com.atguigu.hbase.util.NameSpaceUtils.createNs method param error : " + e);
            return false;
        }
    }

    /**
     * 查询库下的表集合
     *
     * @param conn Hbase连接
     * @param name 库的名称
     * @return org.apache.hadoop.hbase.TableName[]
     * @Author 剧情再美终是戏
     * @Date 2019/12/7
     * @Version 1.0
     **/
    public static TableName[] getTablesByNS(Connection conn, String name) {
        if (null == conn || StringUtils.isBlank(name)) {
            logger.error("com.atguigu.hbase.util.NameSpaceUtils.getTableListByNS method param error : conn, name  is require!");
            return null;
        }

        try (Admin admin = conn.getAdmin()) {
            TableName[] tableNames = admin.listTableNamesByNamespace(name);
            return tableNames;
        } catch (Exception e) {
            logger.error("com.atguigu.hbase.util.NameSpaceUtils.getTableListByNS method happen error : " + e);
            return null;
        }
    }

    /**
     * 删除库
     *
     * @param conn Hbase连接
     * @param name 库的名称
     * @return boolean
     * @Author 剧情再美终是戏
     * @Date 2019/12/8
     * @Version 1.0
     **/
    public static boolean dropNS(Connection conn, String name) {
        if (null == conn || StringUtils.isBlank(name)) {
            logger.error("com.atguigu.hbase.util.NameSpaceUtils.dropNS method param error : conn, name  is require!");
            return false;
        }
        // 查询库是否存在
        if (!existsNS(conn, name)) {
            logger.error("com.atguigu.hbase.util.NameSpaceUtils.dropNS method Warn : nameSpace(" + name + ") is not exists");
            return false;
        }

        // 判断库下有没有表
        TableName[] tablesByNS = getTablesByNS(conn, name);
        if (ArrayUtils.isNotEmpty(tablesByNS)) {
            logger.error("com.atguigu.hbase.util.NameSpaceUtils.dropNS method Warn : nameSpace(" + name + ") is not empty");
            return false;
        }

        // 如果库下没有表就可以进行删除操作
        try (Admin admin = conn.getAdmin()) {
            admin.deleteNamespace(name);
        } catch (Exception e) {
            logger.error("com.atguigu.hbase.util.NameSpaceUtils.dropNS method happen error : " + e);
        }
        return true;
    }

    /**
     * 查询库集合
     *
     * @param conn Hbase连接
     * @return org.apache.hadoop.hbase.NamespaceDescriptor[]
     * @Author 剧情再美终是戏
     * @Date 2019/12/8
     * @Version 1.0
     **/
    public static NamespaceDescriptor[] listNS(Connection conn) {
        if (null == conn) {
            logger.error("com.atguigu.hbase.util.NameSpaceUtils.listNS method param error : conn is require!");
            return null;
        }

        // 查询库的集合
        try (Admin admin = conn.getAdmin()) {
            return admin.listNamespaceDescriptors();
        } catch (Exception e) {
            logger.error("com.atguigu.hbase.util.NameSpaceUtils.listNS method happen error : " + e);
        }
        return null;
    }


    /**
     * 查询库是否存在
     *
     * @param conn Hbase连接
     * @param name 库的名称
     * @return boolean
     * @Author 剧情再美终是戏
     * @Date 2019/12/8
     * @Version 1.0
     **/
    public static boolean existsNS(Connection conn, String name) {
        if (null == conn || StringUtils.isBlank(name)) {
            logger.error("com.atguigu.hbase.util.NameSpaceUtils.existsNS method param error : conn, name  is require!");
            return false;
        }

        // 查询库是否存在
        try (Admin admin = conn.getAdmin()) {
            admin.getNamespaceDescriptor(name);
            return true;
        } catch (Exception e) {
            logger.error("com.atguigu.hbase.util.NameSpaceUtils.existsNS method happen error : " + e);
            return false;
        }
    }
}
